/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * InventoryEditor Class:
 * This class is used to edit an inventory item in the current inventory list, hence making it available for use in various products
 * @author Vale
 *
 */
public class InventoryEditor implements ActionListener, WindowListener {
    private JFrame editInvFrame;
    private JPanel headingPanel;
    private JPanel buttonPanel;
    private JPanel centralPanel;
    private JButton doneButton;
    private JButton cancelButton;
    private InventoryList inventory;
    private JTextField invName;
    private JTextField invStock;
    private JTextField invQuality;
    private JTextField invUnits;
    private JTextField invCost;
    private Inventory currInv;
    private Inventory invRestore;
    private JFrame currViewer;
    private ProductList products;
    private static final String HEADING_FONT = "Heading";
    private static final int HEADING_FONT_SIZE = 20;
    private static final String FRAME_HEADING = "New Inventory Item";
    private static final String GUI_TITLE = "Edit Inventory Item";
    private static final String DONE_BUTTON = "Done";
    private static final String CANCEL_BUTTON = "Cancel";
    private static final int BPANEL_ROWS = 1;
    private static final int BPANEL_COLS = 2;
    private static final int BUTTON_SPACING = 5;
    private static final int CPANEL_ROWS = 6;
    private static final int CPANEL_COLS = 2;
    private static final String INVNAME_LAB = "Inventory Name";
    private static final String INVCOST_LAB = "Inventory Cost/Unit";
    private static final String INVSTOCK_LAB = "Inventory Stock";
    private static final String INVQUAL_LAB = "Inventory Quality (0-5)";
    private static final String INVUNITS_LAB = "Inventory Units";
    private final String ERR = "Error";
    private final String INCORRECT_INPUT = "Incorrect Input";
    private static final String EMPTY_STRING = "";


    /**
     * InventoryEditor Constructor:
     * This is the constructor for the InventoryEditor class. It takes a number of inputs, which are used to edit the current inventory item, such that it may be available for use
     * in various products.
     *
     */
    public InventoryEditor(Inventory invItem, InventoryList invList, ProductList prodList, JFrame currFrame) {
        //Create new Objects
        currViewer = currFrame;
        products = prodList;
        inventory = invList;
        invRestore = invItem;
        currInv = invItem;
        invList.remove(currInv);
        invName = new JTextField();
        invCost = new JTextField();
        invStock = new JTextField();
        invQuality = new JTextField();
        invUnits = new JTextField();
        editInvFrame = new JFrame(FRAME_HEADING);
        headingPanel = new JPanel();
        buttonPanel = new JPanel();
        doneButton = new JButton(DONE_BUTTON);
        cancelButton = new JButton(CANCEL_BUTTON);
        centralPanel = new JPanel();

        //Edit Frames properties
        editInvFrame.addWindowListener(this);
        editInvFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        editInvFrame.setLayout(new BorderLayout());

        //Format Heading
        JTextArea heading = new JTextArea(GUI_TITLE);
        Color bgColour = editInvFrame.getBackground();
        heading.setBackground(bgColour);
        Font headerFont = new Font(HEADING_FONT, Font.BOLD, HEADING_FONT_SIZE);
        heading.setFont(headerFont);
        headingPanel.add(heading);

        //Add Buttons
        buttonPanel.setLayout(new GridLayout(BPANEL_ROWS,BPANEL_COLS,BUTTON_SPACING,BUTTON_SPACING));
        buttonPanel.add(doneButton);
        doneButton.addActionListener(new DoneButton());
        buttonPanel.add(cancelButton);
        cancelButton.addActionListener(this);

        //Set up central Pane
        centralPanel.setLayout(new GridLayout(CPANEL_ROWS,CPANEL_COLS,BUTTON_SPACING,BUTTON_SPACING));
        centralPanel.add(new JLabel(INVNAME_LAB));
        invName.setText(currInv.getInvName());
        centralPanel.add(invName);
        centralPanel.add(new JLabel(INVQUAL_LAB));
        invQuality.setText(String.valueOf(currInv.getInvQuality()));
        centralPanel.add(invQuality);
        centralPanel.add(new JLabel(INVSTOCK_LAB));
        invStock.setText(String.valueOf(currInv.getInvStock()));
        centralPanel.add(invStock);
        centralPanel.add(new JLabel(INVUNITS_LAB));
        invUnits.setText(currInv.getInvUnits());
        centralPanel.add(invUnits);
        centralPanel.add(new JLabel(INVCOST_LAB));
        invCost.setText(String.valueOf(currInv.getInvCost()));
        centralPanel.add(invCost);

        //Add content to frame
        editInvFrame.getContentPane().add(headingPanel, BorderLayout.NORTH);
        editInvFrame.getContentPane().add(centralPanel, BorderLayout.CENTER);
        editInvFrame.getContentPane().add(buttonPanel,BorderLayout.SOUTH);
        editInvFrame.setVisible(true);
        editInvFrame.setResizable(false);
        editInvFrame.pack();
    }

    /**
     * DoneButton Inner Class:
     * This inner class is used as a listener to the "Done" button. It is used to confirm the changes made to the inventory item.
     *
     */
    public class DoneButton implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            currInv = new Inventory();
            currInv.setInvName(invName.getText());
            currInv.setInvUnits(invUnits.getText());
            try {
            currInv.setInvQuality(Integer.parseInt(invQuality.getText()));
            currInv.setInvStock(Integer.parseInt(invStock.getText()));
            currInv.setInvCost(Double.parseDouble(invCost.getText()));
            } catch (NumberFormatException x) {
                JOptionPane.showMessageDialog(editInvFrame, INCORRECT_INPUT, ERR, JOptionPane.ERROR_MESSAGE);
                return;
            }
            inventory.add(currInv);
            editInvFrame.dispose();
            currViewer.dispose();
            new InventoryViewer(inventory,products);
        }

    }

    /**
     * void actionPerformed(ActionEvent e) Method:
     * This is called when the "Cancel" button is pressed. It calls dispose(), which frees up all currently used resources by the current frame (viewProdFrame)
     * and consequently closes it.
     *
     */
    public void actionPerformed(ActionEvent e) {
        inventory.add(invRestore);
        editInvFrame.dispose();
    }

    /**
     * void windowOpened(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has not been implemented.
     *
     */
    public void windowOpened(WindowEvent e) {
    }

    /**
     * void windowClosing(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has been implemented such that if the frame is closed without using the specified "Back"
     * button, the resources used by the frame are released.
     *
     */
    public void windowClosing(WindowEvent e) {
        editInvFrame.dispose();
    }

    /**
     * void windowClosed(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has not been implemented, as its purpose has been covered by the 'windowClosing' event.
     *
     */
    public void windowClosed(WindowEvent e) {
    }

    /**
     * void windowIconified(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has not been implemented.
     *
     */
    public void windowIconified(WindowEvent e) {
    }

    /**
     * void windowDeiconified(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has not been implemented.
     *
     */
    public void windowDeiconified(WindowEvent e) {
    }

    /**
     * void windowActivated(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has not been implemented.
     *
     */
    public void windowActivated(WindowEvent e) {
    }

    /**
     * void windowDeactivated(WindowEvent e) Method:
     * This is one of the abstract methods of the WindowListener class. It has not been implemented.
     *
     */
    public void windowDeactivated(WindowEvent e) {
    }
}
